/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.openbravo.bean.erp;

import cn.ekuma.data.dao.bean.I_ModifiedLogBean;
import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.PaymentInfo;
import com.openbravo.bean.erp.ordercalc.OrderCalcItem;
import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.bean.shard.IncludeTaxs;
import com.openbravo.def.I_CalcTaxes;
import com.openbravo.def.I_TaxCalcLine;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 *
 * 双重单据正反相：如对货商的入库单（普通货商入库单《==》货商入库退货单)
 */
public class Order implements I_ModifiedLogBean<String>,I_CalcTaxes{

    static final long serialVersionUID=-2807576535053645676L;

    public static final int ORDERBEARING_NORMAL = 0;
    public static final int ORDERBEARING_REVERSE = 1;

    public static final String AUTOCREATE_NAME="autoName";

    public static final String CONSULTID_Name="ConsultID";//用于读取和设置，当前单据的参照项（如可以是其它单据或是某一个时间的库存引用--用与盘点单时

    public static final String CONSULT_ProductName="ProductName";

    public static final String LOCATION_name="locationName";
    public static final String LOCATION_name1="locationName1";
    
    public static final String ATTRIBUTESET_InstDescription="arrtDesc";

    public String m_sId;
    public int orderBearing;
    public String m_iOrderId;
    public java.util.Date m_dDate;
    public HashMap<String,String> attributes;
    public UserInfo m_User;
    public BusinessPartner m_Customer;
    public String m_sActiveCash;
    public List<OrderLine> m_aLines;
    public List<PaymentInfo> payments;
    public List<IncludeTaxs> taxes;
    public List<OrderCalcItem> calcItems;
    public String m_sResponse;
    public List<OrderRelation> refOrders;

    public OrderType defineType;//单据原始类型定义
    public double m_dPriceRate;//整单折扣率
    public String attsetinstid;//单据属性实例引用

    public String locationRef;//发生的相关库存地址
    public String locationRef1;//发生的相关库存地址1（只用与内库调拨时有用

    public double totalPrice;
    public double totalTax;
    public double totalConsultPrice;
    public double totalExtendPrice;
    
    public UserInfo reference_User;

    public String memo;//备注

    public List<OrderState> states;

    public String currentState;

    public String consultProductId;//单据商品引用
    public String consultProductAttsetinstid;//商品属性实列
    private Date lastModified;
    
    private double debt;//赊账额
    
    public java.util.Date m_dRefDate;//参照时间
    
    public String customerAddr;//关联地址
    
    public String deliverType;//送货类型
    
    public String deliverTypeDesc;//送货类型说明
    
    public BusinessPartner threeParty;//三方

    /** Creates new OrderModel */
    public Order() {
       this(null,null);
    }

   public Order(OrderType dType,String locRef){
        orderBearing = ORDERBEARING_NORMAL;
        attributes = new HashMap<String,String>();
        m_User = null;
        m_Customer = null;
        m_sActiveCash = null;
        m_aLines = new ArrayList<OrderLine>(); // vacio de lineas

        m_dPriceRate=1;
        defineType=dType;
        locationRef=locRef;
        locationRef1=null;

        payments = new ArrayList<PaymentInfo>();
        refOrders=new ArrayList<OrderRelation>();
        taxes = null;
        m_sResponse = null;

        reference_User=null;
        totalPrice=0;
        states=new ArrayList<OrderState>();
        totalTax=0;
        totalConsultPrice=0;
        debt=0;
        calcItems=new ArrayList<OrderCalcItem>();
        totalExtendPrice=0;
   }

    public String getAttsetinstid() {
        return attsetinstid;
    }

    public void setAttsetinstid(String attsetinstid) {
        this.attsetinstid = attsetinstid;
    }

    public String getId() {
        return m_sId;
    }

    public int getOrderType() {
        return orderBearing;
    }

    public void setOrderType(int Ordertype) {
        this.orderBearing = Ordertype;
    }

    public String getOrderId() {
        return m_iOrderId;
    }

    public void setOrderId(String iOrderId) {
        m_iOrderId = iOrderId;
        refreshLines();
    }

    public java.util.Date getDate() {
        return m_dDate;
    }

    public void setDate(java.util.Date dDate) {
        m_dDate = dDate;
    }

    public UserInfo getUser() {
        return m_User;
    }

    public void setUser(UserInfo value) {
        m_User = value;
    }

    public BusinessPartner getCustomer() {
        return m_Customer;
    }

    public void setCustomer(BusinessPartner value) {
        m_Customer = value;
    }

    public String getCustomerId() {
        if (m_Customer == null) {
            return null;
        } else {
            return m_Customer.getId();
        }
    }

    public void setActiveCash(String value) {
        m_sActiveCash = value;
    }

    public String getActiveCash() {
        return m_sActiveCash;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }
    
    public String getProperty(String key) {
        return attributes.get(key);
    }

    public String getProperty(String key, String defaultvalue) {
        String ret=attributes.get(key);
        return ret!=null?ret:defaultvalue;
    }

    public void setProperty(String key, String value) {
        attributes.put(key, value);
    }

    public HashMap getProperties() {
        return attributes;
    }

    public OrderLine getLine(int index) {
        return m_aLines.get(index);
    }

    public void addLine(OrderLine oLine) {

        oLine.setOrder(m_sId, m_aLines.size());
        m_aLines.add(oLine);
    }

    public void insertLine(int index, OrderLine oLine) {
        m_aLines.add(index, oLine);
        refreshLines();
    }

    public void setLine(int index, OrderLine oLine) {
        oLine.setOrder(m_sId, index);
        m_aLines.set(index, oLine);
    }

    public void removeLine(int index) {
        m_aLines.remove(index);
        refreshLines();
    }

    public void refreshLines() {
        for (int i = 0; i < m_aLines.size(); i++) {
            getLine(i).setOrder(m_sId, i);
        }
    }

    public int getLinesCount() {
        return m_aLines.size();
    }

    public OrderType getDefineType() {
        return defineType;
    }

    public void setDefineType(OrderType defineType) {
        this.defineType = defineType;
    }


    public List<OrderLine> getLines() {
        return m_aLines;
    }

    public UserInfo getReference_User() {
        return reference_User;
    }

    public void setReference_User(UserInfo reference_User) {
        this.reference_User = reference_User;
    }
    
    public void setLines(List<OrderLine> l) {
        m_aLines = l;
        refreshLines();
    }

    public List<PaymentInfo> getPayments() {
        return payments;
    }

    public void setPayments(List<PaymentInfo> l) {
        payments = l;
    }

    public void resetPayments() {
        payments = new ArrayList<PaymentInfo>();
    }


    public boolean hasTaxesCalculated() {
        return taxes != null;
    }

    public void setTaxes(List<IncludeTaxs> l) {
        taxes = l;
    }

    public void resetTaxes() {
        taxes = null;
    }

    public double getM_dPriceRate() {
        return m_dPriceRate;
    }

    public void setM_dPriceRate(double rate) {
        m_dPriceRate = rate;
    }

    public String getLocationRef() {
        return locationRef;
    }

    public void setLocationRef(String locationRef) {
        this.locationRef = locationRef;
    }

    public String getLocationRef1() {
        return locationRef1;
    }

    public void setLocationRef1(String locationRef1) {
        this.locationRef1 = locationRef1;
    }


    public boolean isConsult(){
        String refID=getProperty(CONSULTID_Name,null);
        if(refID==null||refID.isEmpty())
            return false;
        return true;
    }

    public String getConsultRef(){
        return getProperty(CONSULTID_Name,null);
    }

    public void setConsultRef(String consultRef){
        if(consultRef==null)
            setProperty(CONSULTID_Name, "");
       setProperty(CONSULTID_Name, consultRef);
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

   public List<IncludeTaxs> getTaxes() {
        return taxes;
    }
   
   public String getLocationDesc(){
       return getProperty(LOCATION_name, "");
   }
   
   public void setLocationDesc(String locName){
       attributes.put(LOCATION_name, locName);
   }
   
   public String getLocationDesc1(){
       return getProperty(LOCATION_name1, "");
   }
   
   public void setLocationDesc1(String locName){
       attributes.put(LOCATION_name1, locName);
   }

   public String getOrderAttSetInstDesc() {
        return getProperty(ATTRIBUTESET_InstDescription, "");
    }

    public List<OrderRelation> getRefOrders() {
        return refOrders;
    }

    public void setRefOrders(List<OrderRelation> refOrders) {
        this.refOrders = refOrders;
        if(this.refOrders==null)
            this.refOrders=new ArrayList();
    }

    public String getCurrentState() {
        return currentState;
    }
    

    public int getOrderBearing() {
		return orderBearing;
	}

	public void setOrderBearing(int orderBearing) {
		this.orderBearing = orderBearing;
	}

	public String getM_sResponse() {
		return m_sResponse;
	}

	public void setM_sResponse(String m_sResponse) {
		this.m_sResponse = m_sResponse;
	}

	public void setCurrentState(String currentState) {
		this.currentState = currentState;
	}

	public List<OrderState> getStates() {
        return states;
    }

    public void setStates(List<OrderState> states) {
       this.states = states;
        if(this.states==null)
            this.states=new ArrayList();
    }

    public void addState(OrderState state){
        state.setOrderID(m_sId);
        this.currentState=state.getState();
        states.add(state);
    }

    public String getConsultProductAttsetinstid() {
        return consultProductAttsetinstid;
    }

    public void setConsultProductAttsetinstid(String consultProductAttsetinstid) {
        this.consultProductAttsetinstid = consultProductAttsetinstid;
    }

    public String getConsultProductId() {
        return consultProductId;
    }

    public void setConsultProductId(String consultProductId) {
        this.consultProductId = consultProductId;
    }

    public String getConsultProductName(){
        return getProperty(CONSULT_ProductName,null);
    }
    
    public void setConsultProductName(String prodDest){
        setProperty(CONSULT_ProductName, prodDest);
    }

    public void setID(String id) {
        m_sId=id;
    }

    public String getKey() {
        return m_sId;
    }

    public Date getLastModified() {
        return lastModified;
    }

    public void setLastModified(Date lastModified) {
        this.lastModified = lastModified;
    }

    public void setKey(String key) {
        this.m_sId=key;
    }

    public List<I_TaxCalcLine> getTaxCalcLines() {
        List<I_TaxCalcLine> line=new ArrayList<I_TaxCalcLine>();
        for(OrderLine l:m_aLines)
            line.add(l);
       return line;
    }

    public int getTaxType() {
        return TAXTYPE_Order;
    }

	public double getTotalTax() {
		return totalTax;
	}

	public void setTotalTax(double totalTax) {
		this.totalTax = totalTax;
	}

	public double getTotalConsultPrice() {
		return totalConsultPrice;
	}

	public void setTotalConsultPrice(double totalConsultPrice) {
		this.totalConsultPrice = totalConsultPrice;
	}

	public HashMap<String, String> getAttributes() {
		return attributes;
	}

	public void setAttributes(HashMap<String, String> attributes) {
		this.attributes = attributes;
	}

	public double getDebt() {
		return debt;
	}

	public void setDebt(double debt) {
		this.debt = debt;
	}

	public java.util.Date getM_dRefDate() {
		return m_dRefDate;
	}

	public void setM_dRefDate(java.util.Date m_dRefDate) {
		this.m_dRefDate = m_dRefDate;
	}

	public double getTotalExtendPrice() {
		return totalExtendPrice;
	}

	public void setTotalExtendPrice(double totalExtendPrice) {
		this.totalExtendPrice = totalExtendPrice;
	}

	public List<OrderCalcItem> getCalcItems() {
		return calcItems;
	}

	public void setCalcItems(List<OrderCalcItem> calcItems) {
		this.calcItems = calcItems;
	}

	public String getCustomerAddr() {
		return customerAddr;
	}

	public void setCustomerAddr(String customerAddr) {
		this.customerAddr = customerAddr;
	}

	public String getDeliverType() {
		return deliverType;
	}

	public void setDeliverType(String deliverType) {
		this.deliverType = deliverType;
	}

	public String getDeliverTypeDesc() {
		return deliverTypeDesc;
	}

	public void setDeliverTypeDesc(String deliverTypeDesc) {
		this.deliverTypeDesc = deliverTypeDesc;
	}

	public BusinessPartner getThreeParty() {
		return threeParty;
	}

	public void setThreeParty(BusinessPartner threeParty) {
		this.threeParty = threeParty;
	}

	
	
	
}
